﻿
namespace CmdArgs.Validation.Options
{
    using System.ComponentModel;

    /// <summary>
    /// The generic validator uses namespace System.ComponentModel, 
    /// especially <see cref="TypeDescriptor"/> to determine whether the conversion
    /// to specified type is possible.
    /// </summary>
    /// <typeparam name="T">The target type for conversion.</typeparam>
    public sealed class GenericOptionValidator<T> : ValueValidatorTemplate
    {
        protected override ValidationResult Validate(string value, CmdArgs.Settings.OptionSettings optionSettings)
        {
            if (TypeDescriptor.GetConverter(typeof(T)).IsValid(value))
            {
                return ValidationResult.GetValidResult();
            }
            else
            {
                return new ValidationResult(
                    new ValidationError(
                        string.Format("Value {0} is not valid for this parameter", value),
                        optionSettings));
            }
        }
    }
}
